package gui.nodes;

import gui.AbstractViewPane;
import gui.AnneeEtudeUIConsulter;
import gui.FormationUIConsulter;
import gui.MainController;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;

import utility.Record;
import business.FormationBL;

public class FormationNode extends AbstractNode {

	int formationId;
	String formationName;
	DefaultMutableTreeNode formationNode;
	AbstractNode parentNode;
	
	public FormationNode(FormationRoot parentNode, Record recordFormation) {
		this.parentNode = parentNode;
		this.formationId = recordFormation.getId();
		this.formationName = recordFormation.getLibelle();
	}

	@Override
	public JPopupMenu makePopup() {
		return new Popup();
	}

	@Override
	public String getNodeName() {
		return formationName;
	}

	public void loadPrimaryViewPane() {
		AbstractViewPane newViewPane = new FormationUIConsulter(parentNode,
				AbstractViewPane.ViewMode.CONSULTATION, formationId);
		MainController.getInstance().setViewPane(newViewPane);
	}

	public void loadModifierViewPane() {
		AbstractViewPane newViewPane = new FormationUIConsulter(parentNode,
				AbstractViewPane.ViewMode.MODIFICATION, formationId);
		MainController.getInstance().setViewPane(newViewPane);
	}

	public void loadAjouterViewPane() {
		AbstractViewPane newViewPane = new AnneeEtudeUIConsulter(this,
				AbstractViewPane.ViewMode.AJOUT, -1, formationId);
		MainController.getInstance().setViewPane(newViewPane);
	}

	public void loadSupprimerViewPane() {
		if (supprimer() == JOptionPane.YES_OPTION) {
			FormationBL formationBL = new FormationBL(formationId);
			formationBL.supprimerFormation();
		}
		((AbstractNode)parent).refresh();
	}

	private class Popup extends JPopupMenu implements ActionListener {
		JMenuItem consulter = new JMenuItem("Consulter les informations");
		JMenuItem ajouter = new JMenuItem("Ajouter une année d'étude");
		JMenuItem modifier = new JMenuItem("Modifier la formation");
		JMenuItem supprimer = new JMenuItem("Supprimer la formation");

		public Popup() {
			consulter.addActionListener(this);
			ajouter.addActionListener(this);
			modifier.addActionListener(this);
			supprimer.addActionListener(this);
			add(consulter);
			add(ajouter);
			add(modifier);
			add(supprimer);
		}

		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == consulter) {
				loadPrimaryViewPane();
			}
			if (e.getSource() == ajouter) {
				loadAjouterViewPane();
			}
			if (e.getSource() == modifier) {
				loadModifierViewPane();
			}
			if (e.getSource() == supprimer) {
				loadSupprimerViewPane();
			}

		}

	}

	@Override
	public void expandHook() {
		this.removeAllChildren();

		FormationBL formationBL = new FormationBL(formationId);

		for (Record ae : formationBL.getListeAnnees()) {
			AnneeEtudeNode aeNode = new AnneeEtudeNode(this,ae, formationId);
			add(aeNode);
		}
		
		
	}

	public int supprimer() {
		return JOptionPane.showConfirmDialog(
			    null,
			    "Êtes vous sûr de vouloir supprimer la formation\n"+
			    //ensBL.getName()+" "+ensBL.getPrenom()+
			    " ?",
			    "Avertissement",
			    JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
		
	}

}
